package cn.wolfcode.trip.base.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class TravelController {

    @Autowired
    private ITravelService travelService;

    /**
     * 查询审核游记页面
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("travelAudit/list")
    public String list(Model model, @ModelAttribute ("qo") TravelQueryObject qo){
        if(qo.getState()==null){
            //待发布游记
            qo.setState(Travel.STATE_WAIT);
        }

        //公开的游记
        qo.setIsPublic(true);

        //创建时间正序
        qo.setOrderBy("t.createTime asc");

        //执行查询前,先设置条件
        model.addAttribute("result", travelService.querForList(qo));

        return "travel/audit";
    }

    /**
     * 根据id查询游记内容
     */
    @RequestMapping("travel/getContentById")
    @ResponseBody
    public TravelContent getContentById(Long id){
        return travelService.getContentById(id);
    }

    /**
     * 修改游记状态
     */
    @RequestMapping("travel/changeState")
    @ResponseBody
    public JSONResult changeState(Travel travel){
        JSONResult result = new JSONResult();

        try {
            travelService.changState(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }

    /**
     * 查询已发布的游记页面
     */
    @RequestMapping("travelRelease/list")
    public String release(Model model,@ModelAttribute("qo") TravelQueryObject qo){
        //设置显示已发布的游记
        qo.setState(Travel.STATE_RELEASE);
        //设置游记是否公开(IsPublic)
        qo.setIsPublic(true);
        //按发布时间倒序
        qo.setOrderBy("t.releaseTime desc");
        model.addAttribute("result",travelService.querForList(qo));
        return "travel/release";
    }



}
